// @ts-nocheck
import { sourceData } from '@@/hw-auth/sourceData';
import { AcModal, ItemAuthModal ,authMatchEnum} from '@@/hw-auth/modal.d';
import { authData } from '@@/hw-auth/fns/sourceDataProvider';
const { appKey } = sourceData;

interface AuthModal {
  appKeyList?: string[];
  authData?: string | null;
  type?: number;
}
type AuthTypeModal = 'route' | 'component' | 'api';
class AuthProvider {
  appKeys: string[] = [];

  isSuperAdmin: boolean = true;

  childAuthData: Record<string, string[]> = {};

  rootAuthData: string[] = [];

  setData(data: AuthModal) {
    const { appKeyList = [], authData, type } = data;
    this.isSuperAdmin = type === 0;
    this.appKeys = appKeyList;
    const authObj = this.toJson(authData);
    this.rootAuthData = authObj[appKey];
    Reflect.deleteProperty(authObj, appKey);
    this.childAuthData = { ...authObj };
  }

  upData(data: AuthModal) {
    const { appKeyList, authData, type } = data;
    if (appKeyList) {
      this.appKeys = appKeyList;
    }

    if (typeof type === 'number') {
      this.isSuperAdmin = type === 0;
    }

    if (authData) {
      const authObj = this.toJson(authData);
      this.rootAuthData = authObj[appKey];
      Reflect.deleteProperty(authObj, appKey);
      this.childAuthData = { ...authObj };
    }
  } //更新数据

  removeData() {
    this.appKeys = [];
    this.isSuperAdmin = true;
    this.rootAuthData = [];
    this.childAuthData = {};
  } //删除数据

  toJson(jsonStr?: string | null) {
    if (!jsonStr) {
      return {};
    }
    try {
      return JSON.parse(jsonStr);
    } catch (e) {
      return {};
    }
  }

  checkRoute(path: string) {
    const { routes } = authData.data;
    const item = routes[path];
    return this.checkAuth(item?.auth);
  } //路由权限验证

  checkApi(path: string) {
    const { apis } = authData.data;
    const apiAuth = apis[path];
    return this.checkAuth(apiAuth, 'api');
  } //api验证

  checkComponent(auth: string) {
    const { components } = authData.data;
    const componentAuth = components[auth];
    return this.checkAuth(componentAuth, 'component');
  } //组件权限验证

  checkAuth(auth: AcModal, type: AuthTypeModal = 'route') {
    if (typeof auth === 'undefined') {
      return authMatchEnum.notFind;
    }
    if (!auth) {
      return authMatchEnum.pass;
    }
    if (typeof auth === 'object') {
      return this.checkPkey(auth, type);
    }
    const authData = this.rootAuthData;
    return this.matchAuth(authData, auth);
  }

  checkPkey(auth: ItemAuthModal, type?: AuthTypeModal) {
    const { pkey, key = '' ,isChild} = auth;
    let pass = authMatchEnum.pass;
    if (pkey) {
      pass = this.matchAuth(this.appKeys, pkey);
    }//验证子应用
    if (pass === authMatchEnum.notPass) {
      return pass;
    }
    if (this.isSuperAdmin) {
      return authMatchEnum.pass;
    }
    let pKeyAuthData = this.rootAuthData;
    if (type === 'route' && isChild) {
      pKeyAuthData = this.childAuthData[pkey];
    }
    return this.matchAuth(pKeyAuthData, key);
  } //包含pkey的验证，pKey>superAdmin

  matchAuth(authData: string[], auth: string) {
    const index = authData.indexOf(auth);
    if (index === -1) {
      return authMatchEnum.notPass;
    }
    return authMatchEnum.pass;
  }
}
export const checkerSource = () => {
  const checkAuth = new AuthProvider();
  const obj = {
    childAuthData: checkAuth.childAuthData,
    rootAuthData: checkAuth.rootAuthData,
    set: (data: AuthModal) => {
      checkAuth.setData(data);
      obj.childAuthData = checkAuth.childAuthData;
      obj.rootAuthData = checkAuth.rootAuthData;
    },
    up: (data: AuthModal) => {
      checkAuth.upData(data);
      obj.childAuthData = checkAuth.childAuthData;
      obj.rootAuthData = checkAuth.rootAuthData;
    },
    remove: () => {
      checkAuth.removeData();
      obj.childAuthData = checkAuth.childAuthData;
      obj.rootAuthData = checkAuth.rootAuthData;
    },
    check: (key: string, type: 'route' | 'api' | 'component' = 'route') => {
      switch (type) {
        case 'api':
          return checkAuth.checkApi(key);
        case 'component':
          return checkAuth.checkComponent(key);
        case 'route':
          return checkAuth.checkRoute(key);
      }
    },
  };
  return new Proxy(obj, {
    set() {
      console.error('请勿修改对象，调用set方法!');
      return false;
    },
  });
};
